package com.youxue.teachin.user.resetpassword;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import com.ali.music.navigator.backstack.Params;
import com.library.rest.ApiServiceManager;
import com.library.rest.rxjava.RxResultHelper;
import com.library.rest.rxjava.RxSchedulersHelper;
import com.library.rest.vo.ResponseModel;
import com.library.uiframework.activity.ActionBarLayoutActivity;
import com.library.uiframework.utils.MToast;
import com.library.uiframework.utils.PatternUtils;
import com.library.uiframework.widget.actionbar.ActionBarLayout.Action;
import com.youxue.teachin.R;
import com.youxue.teachin.common.utils.ActivityCacheHelper;
import com.youxue.teachin.common.utils.DefaultSubscriber;
import com.youxue.teachin.user.repository.IUserRepository;
import com.youxue.teachin.user.userSystem.UserSystem;
import rx.Observable;

import static com.library.uiframework.slidingclose.SlidingClosableRelativeLayout.SLIDING_CLOSE_MODE_NONE;

/**
 * 在此写用途
 *
 * @author: zhiwei
 * @date: 2017-10-05 17:14
 * @version: 9.1.0
 */
public class ResetPasswordActivity2 extends ActionBarLayoutActivity implements OnClickListener {

    private EditText mLoginPassword;
    private ImageView mShowPassword;
    private TextView mRegistBt;
    private ImageView mLoginPasswordIcon;
    private View mLoginPasswordLine;

    private String mPhone;
    private String mCaptcha;
    /**
     * 1  忘记密码
     * 2 修改密码(登录后,设置中进行密码修改)
     */
    private int mFromType;

    @Override
    protected void onActionClick(int actionId, Action action) {
        super.onActionClick(actionId, action);

    }

    @Override
    protected void onInitActionBar() {
        super.onInitActionBar();
        setTitle("密码重置");
    }

    @Override
    protected Action createBack() {
        return addLeftImageAction(ACTION_BACK, R.drawable.back);
    }

    @Override
    protected boolean needMenuAction() {
        return false;
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        initView();
        getActionBarLayout().setFloatingModeAlpha(0);
        getActionBarLayout().getTitleView().setAlpha(1);
        setTitleColor(getResources().getColor(R.color.cl_1));
        setSlidingCloseMode(SLIDING_CLOSE_MODE_NONE);
        Params params = getParams();
        mPhone = params.getString("phone", "");
        mCaptcha = params.getString("captcha", "");
        mFromType = params.getInt("type", 1);
    }

    @Override
    protected View onCreateContentView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.user_reset_password_activity2, container, false);
        return view;
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.regist_bt) {
            next();
        }
    }

    private void next() {
        String password = mLoginPassword.getText().toString();
        if (!PatternUtils.isPassword(password)) {
            MToast.getInstance().setText("密码支持大小写字母、数据");
            return;
        }
        Class<IUserRepository> clazz = IUserRepository.class;
        Observable<ResponseModel<Void>> observable = null;
        if (mFromType == 1) {
            observable = ApiServiceManager.getOrCreateApiService(clazz)
                .forgetPassword(mPhone, password, mCaptcha);
        } else {
            observable = ApiServiceManager.getOrCreateApiService(clazz)
                .updatePassword(password, mCaptcha);
        }

        observable
            .compose(RxSchedulersHelper.<ResponseModel<Void>>io_main())
            .compose(RxResultHelper.<Void>handleResult())
            .subscribe(new DefaultSubscriber<Void>() {
            @Override
            public void onNext(Void responseModel) {
                super.onNext(responseModel);
                MToast.getInstance().setText("修改密码成功");
                if (mFromType == 1) {
                    UserSystem.getInstance().login();
                }
                ActivityCacheHelper.finish();
                finish();
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
            }
        });
    }

    private void initView() {
        mLoginPassword = (EditText)findViewById(R.id.login_password);
        mShowPassword = (ImageView)findViewById(R.id.show_password);
        mRegistBt = (TextView)findViewById(R.id.regist_bt);
        mLoginPasswordIcon = (ImageView)findViewById(R.id.login_password_icon);
        mLoginPasswordLine = (View)findViewById(R.id.login_password_line);

        mRegistBt.setOnClickListener(this);
        mLoginPasswordIcon.setVisibility(View.GONE);
        mLoginPasswordLine.setVisibility(View.GONE);
        mLoginPassword.setHint("6-20位密码");
    }
}
